var t, e, a, i, n, s, r, o = require("../../../@babel/runtime/helpers/interopRequireDefault"), d = o(require("../../../@babel/runtime/regenerator")), u = require("../../../@babel/runtime/helpers/defineProperty"), c = require("../../../@babel/runtime/helpers/asyncToGenerator"), l = o(require("../../../utils/api")), p = getApp();

Page({
    data: {
        statusBarHeight: p.globalData.statusBarHeight,
        resourceUrl: p.globalData.resourceUrl,
        countdown: "",
        timeLimit: 0,
        interval: null,
        timeout: null,
        startTime: 0,
        endTime: 0,
        headImg: "",
        name: "",
        completeNum: 0,
        op: {},
        step: "step1",
        indexNum: 0,
        endNum: 0,
        questionId: "",
        questionType: 0,
        questionTypeName: "",
        imageArray: [],
        options: [],
        answer: "",
        analysis: "",
        answerState: 0,
        disable: !1,
        correctIndex: -1,
        errorIndex: -1,
        answerProgress: 1,
        correct: 0,
        pkResult: null,
        finished: !1,
        heartTimeout: null
    },
    debounce: function(t, e) {
        var a = arguments.length > 2 && void 0 !== arguments[2] && arguments[2], i = null;
        return function() {
            var n = arguments, s = this;
            i && clearTimeout(i), a ? (i || t.apply(this, arguments), i = setTimeout(function() {
                i = null;
            }, e)) : i = setTimeout(function() {
                t.apply(s, n);
            }, e);
        };
    },
    changeCountDown: function(t) {
        var e = this, a = parseInt(t / 60), i = parseInt(t % 60), n = 0;
        return this.setData({
            startTime: new Date(),
            countdown: (a < 10 ? "0" + a : a) + ":" + (i < 10 ? "0" + i : i)
        }), setInterval(c(d.default.mark(function t() {
            var s, r, o, c, p, h;
            return d.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (0 !== i) {
                        t.next = 20;
                        break;
                    }
                    if (0 !== a) {
                        t.next = 18;
                        break;
                    }
                    if (!e.data.finished) {
                        t.next = 4;
                        break;
                    }
                    return t.abrupt("return");

                  case 4:
                    if (s = wx.getStorageSync("openid"), e.data.activityId) {
                        t.next = 10;
                        break;
                    }
                    return t.next = 8, l.default.finishAnswer({
                        openid: s,
                        totalTime: 1e3 * e.data.timeLimit,
                        type: 4
                    });

                  case 8:
                    t.next = 12;
                    break;

                  case 10:
                    return t.next = 12, l.default.activityFinish({
                        openid: s,
                        activityId: e.data.activityId,
                        totalTime: 1e3 * e.data.timeLimit,
                        type: 4
                    });

                  case 12:
                    return r = setTimeout(function() {
                        wx.switchTab({
                            url: "/pages/answer/answer"
                        });
                    }, 5e3), console.log("timeout:" + r), e.setData({
                        finished: !0,
                        completeNum: e.data.endNum,
                        timeout: r
                    }), clearInterval(e.data.interval), clearInterval(e.data.heartTimeout), t.abrupt("return");

                  case 18:
                    a--, i = 60;

                  case 20:
                    if (i--, n++, e.setData({
                        countdown: (a < 10 ? "0" + a : a) + ":" + (i < 10 ? "0" + i : i)
                    }), !e.data.op.isRobot) {
                        t.next = 31;
                        break;
                    }
                    if (!((c = e.data.op.totalTime / 1e3) < n)) {
                        t.next = 27;
                        break;
                    }
                    return t.abrupt("return");

                  case 27:
                    p = c / e.data.endNum, e.setData((u(o = {}, "op.progress", n / c.toFixed(0)), u(o, "op.completeNum", n >= (e.data.op.completeNum + 1) * p ? e.data.op.completeNum + 1 : e.data.op.completeNum), 
                    o)), t.next = 32;
                    break;

                  case 31:
                    e.setData((u(h = {}, "op.progress", e.data.op.completeNum / e.data.endNum.toFixed(0)), 
                    u(h, "op.totalTime", n), h));

                  case 32:
                  case "end":
                    return t.stop();
                }
            }, t);
        })), 1e3);
    },
    match: function(t) {
        var e = this;
        this.data.activityId ? wx.connectSocket({
            url: p.globalData.websocketActivityUrl + t + "/" + this.data.activityId
        }) : wx.connectSocket({
            url: p.globalData.websocketUrl + t
        }), wx.onSocketOpen(function(t) {
            e.data.heartTimeout = setInterval(function() {
                wx.sendSocketMessage({
                    data: "heart",
                    fail: function() {
                        clearInterval(e.data.heartTimeout);
                    }
                });
            }, 1e4);
        }), wx.onSocketClose(function(t) {
            console.log("ws-close:" + t.reason);
        }), wx.onSocketMessage(function(t) {
            if (t) {
                var a, i = JSON.parse(t.data);
                if (1 === i.type) e.setData((u(a = {}, "op.isRobot", i.data.isRobot), u(a, "op.correctCount", i.data.correctCount), 
                u(a, "op.totalTime", i.data.totalTime), u(a, "op.headImg", "" === i.data.member.headImg ? "/images/boy.png" : i.data.member.headImg), 
                u(a, "op.name", i.data.member.name), u(a, "op.completeNum", 0), u(a, "step", "step2"), 
                a)), e.getQuestion(); else if (2 === i.type) e.setData(u({}, "op.completeNum", i.data.indexNum)); else if (3 === i.type) {
                    var n;
                    e.setData((u(n = {}, "pkResult.win", i.data.self.winner), u(n, "pkResult.opwin", i.data.self.rivalWinner), 
                    u(n, "pkResult.correct", i.data.self.correctCount), u(n, "pkResult.time", parseFloat(i.data.self.totalTime / 1e3)), 
                    u(n, "pkResult.opCorrect", i.data.rival.correctCount), u(n, "pkResult.opTime", parseFloat(i.data.rival.totalTime / 1e3)), 
                    n));
                    var s = e.data.pkResult;
                    wx.redirectTo({
                        url: "/answer/pages/pkAnswerFinish/pkAnswerFinish?win=" + s.win + "&opwin=" + s.opwin + "&correct=" + s.correct + "&time=" + s.time + "&opCorrect=" + s.opCorrect + "&opTime=" + s.opTime + "&headImg=" + e.data.headImg + "&opHeadImg=" + e.data.op.headImg
                    }), clearInterval(e.data.heartTimeout), clearTimeout(e.data.timeout), wx.closeSocket();
                }
            }
        });
    },
    getQuestion: (r = c(d.default.mark(function t() {
        var e, a, i, n, s;
        return d.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                if (e = wx.getStorageSync("openid"), t.prev = 1, a = null, this.data.activityId) {
                    t.next = 9;
                    break;
                }
                return t.next = 6, l.default.questions({
                    openid: e,
                    type: 4
                });

              case 6:
                a = t.sent, t.next = 12;
                break;

              case 9:
                return t.next = 11, l.default.activityQuestions({
                    openid: e,
                    activityId: this.data.activityId,
                    type: 4
                });

              case 11:
                a = t.sent;

              case 12:
                i = [], n = JSON.parse(a.result.answer.content), Object.keys(n).forEach(function(t) {
                    n[t] && i.push({
                        letter: t,
                        spec: n[t],
                        selected: !1,
                        correct: !1,
                        error: !1
                    });
                }), s = 1 === a.result.answer.questionType ? "单选题" : 2 === a.result.answer.questionType ? "多选题" : "判断题", 
                this.setData({
                    question: a.result.answer.title,
                    questionType: a.result.answer.questionType,
                    questionTypeName: s,
                    imageArray: "" === a.result.answer.images ? [] : a.result.answer.images.split("##"),
                    options: i,
                    answer: a.result.answer.answer,
                    analysis: a.result.answer.analysis,
                    indexNum: a.result.indexNum,
                    endNum: a.result.endNum,
                    questionId: a.result.answer.id,
                    paperId: a.result.paperId
                }), 2 == a.result.answer.questionType && this.setData({
                    answerProgress: 2,
                    disable: !1
                }), t.next = 23;
                break;

              case 20:
                t.prev = 20, t.t0 = t.catch(1), console.log(t.t0);

              case 23:
              case "end":
                return t.stop();
            }
        }, t, this, [ [ 1, 20 ] ]);
    })), function() {
        return r.apply(this, arguments);
    }),
    onLoad: function(t) {
        var e = wx.getStorageSync("openid");
        this.setData({
            startTime: new Date(),
            headImg: p.globalData.userInfo.headImg,
            name: p.globalData.userInfo.name,
            timeLimit: t.limit ? t.limit : p.globalData.pkTimeLimit,
            activityId: t.id
        }), this.match(e), this.commitMultipleChoiceDebounce = this.debounce(this.commitMultipleChoice, 500, !1), 
        this.multipleCompleteDebounce = this.debounce(this.multipleComplete, 500, !1), this.nonMultipleCompleteDebounce = this.debounce(this.nonMultipleComplete, 500, !1);
    },
    onHide: (s = c(d.default.mark(function t() {
        var e, a;
        return d.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                if (!this.data.finished) {
                    t.next = 2;
                    break;
                }
                return t.abrupt("return");

              case 2:
                if (e = new Date(), a = wx.getStorageSync("openid"), this.data.activityId) {
                    t.next = 11;
                    break;
                }
                return t.next = 8, l.default.finishAnswer({
                    openid: a,
                    totalTime: e - this.data.startTime,
                    type: 4
                });

              case 8:
                t.sent, t.next = 14;
                break;

              case 11:
                return t.next = 13, l.default.activityFinish({
                    openid: a,
                    activityId: this.data.activityId,
                    totalTime: e - this.data.startTime,
                    type: 4
                });

              case 13:
                t.sent;

              case 14:
                this.setData({
                    finished: !0
                }), clearInterval(this.data.interval), clearTimeout(this.data.timeout), clearInterval(this.data.heartTimeout), 
                wx.closeSocket(), wx.switchTab({
                    url: "/pages/answer/answer"
                });

              case 20:
              case "end":
                return t.stop();
            }
        }, t, this);
    })), function() {
        return s.apply(this, arguments);
    }),
    onUnload: (n = c(d.default.mark(function t() {
        var e, a;
        return d.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                if (!this.data.finished) {
                    t.next = 2;
                    break;
                }
                return t.abrupt("return");

              case 2:
                if (e = new Date(), a = wx.getStorageSync("openid"), this.data.activityId) {
                    t.next = 11;
                    break;
                }
                return t.next = 8, l.default.finishAnswer({
                    openid: a,
                    totalTime: e - this.data.startTime,
                    type: 4
                });

              case 8:
                t.sent, t.next = 14;
                break;

              case 11:
                return t.next = 13, l.default.activityFinish({
                    openid: a,
                    activityId: this.data.activityId,
                    totalTime: e - this.data.startTime,
                    type: 4
                });

              case 13:
                t.sent;

              case 14:
                this.setData({
                    finished: !0
                }), clearInterval(this.data.interval), clearTimeout(this.data.timeout), clearInterval(this.data.heartTimeout), 
                wx.closeSocket();

              case 19:
              case "end":
                return t.stop();
            }
        }, t, this);
    })), function() {
        return n.apply(this, arguments);
    }),
    toStep3: function(t) {
        if ("home-out" === t.detail.animationName) {
            var e = this.changeCountDown(this.data.timeLimit);
            this.setData({
                step: "step3",
                interval: e
            });
        }
    },
    nonMultipleChoice: (i = c(d.default.mark(function t(e) {
        var a;
        return d.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                if (a = wx.getStorageSync("openid"), !this.data.disable) {
                    t.next = 3;
                    break;
                }
                return t.abrupt("return");

              case 3:
                if (t.prev = 3, this.data.activityId) {
                    t.next = 9;
                    break;
                }
                return t.next = 7, l.default.commitAnswer({
                    openid: a,
                    answer: e.currentTarget.dataset.answer,
                    answerId: this.data.questionId,
                    type: 4,
                    endNum: this.data.endNum,
                    indexNum: this.data.indexNum,
                    paperId: this.data.paperId
                });

              case 7:
                t.next = 11;
                break;

              case 9:
                return t.next = 11, l.default.activityCommit({
                    openid: a,
                    activityId: this.data.activityId,
                    answer: e.currentTarget.dataset.answer,
                    answerId: this.data.questionId,
                    type: 4,
                    endNum: this.data.endNum,
                    indexNum: this.data.indexNum,
                    paperId: this.data.paperId
                });

              case 11:
                e.currentTarget.dataset.answer == this.data.answer ? this.setData({
                    answerState: 1,
                    disable: !0,
                    correctIndex: e.currentTarget.dataset.index
                }) : this.setData({
                    answerState: 2,
                    disable: !0,
                    correctIndex: this.data.answer.charCodeAt(0) - "A".charCodeAt(),
                    errorIndex: e.currentTarget.dataset.index
                }), this.data.indexNum === this.data.endNum && this.setData({
                    answerProgress: 3
                }), t.next = 19;
                break;

              case 15:
                t.prev = 15, t.t0 = t.catch(3), console.log(t.t0), wx.showToast({
                    title: "网络异常",
                    icon: "none",
                    duration: 1e3
                });

              case 19:
              case "end":
                return t.stop();
            }
        }, t, this, [ [ 3, 15 ] ]);
    })), function(t) {
        return i.apply(this, arguments);
    }),
    multipleChoice: function(t) {
        var e = "options[" + t.currentTarget.dataset.index + "].selected";
        this.data.options[t.currentTarget.dataset.index].selected ? this.setData(u({}, e, !1)) : this.setData(u({}, e, !0));
    },
    commitMultipleChoice: (a = c(d.default.mark(function t(e) {
        var a, i, n, s, r, o, c, p;
        return d.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                for (a = wx.getStorageSync("openid"), i = 0, n = 0; n < this.data.options.length; n++) this.data.options[n].selected && i++;
                if (0 !== i) {
                    t.next = 6;
                    break;
                }
                return wx.showToast({
                    title: "尚未选择答案",
                    icon: "none",
                    duration: 1e3
                }), t.abrupt("return");

              case 6:
                for (s = "", n = 0; n < this.data.options.length; n++) r = this.data.options[n].selected, 
                o = this.data.options[n].letter, r && (s += o);
                if (!s) {
                    t.next = 30;
                    break;
                }
                if (t.prev = 9, this.data.activityId) {
                    t.next = 15;
                    break;
                }
                return t.next = 13, l.default.commitAnswer({
                    openid: a,
                    answer: s,
                    answerId: this.data.questionId,
                    type: 4,
                    endNum: this.data.endNum,
                    indexNum: this.data.indexNum,
                    paperId: this.data.paperId
                });

              case 13:
                t.next = 17;
                break;

              case 15:
                return t.next = 17, l.default.activityCommit({
                    openid: a,
                    activityId: this.data.activityId,
                    answer: s,
                    answerId: this.data.questionId,
                    type: 4,
                    endNum: this.data.endNum,
                    indexNum: this.data.indexNum,
                    paperId: this.data.paperId
                });

              case 17:
                for (n = 0; n < this.data.options.length; n++) r = this.data.options[n].selected, 
                o = this.data.options[n].letter, this.data.answer.includes(o) ? (c = "options[" + n + "].correct", 
                this.setData(u({}, c, !0))) : this.data.answer.includes(o) && (p = "options[" + n + "].error", 
                this.setData(u({}, p, !0)));
                if (this.data.indexNum !== this.data.endNum) {
                    t.next = 23;
                    break;
                }
                return this.setData({
                    answerState: s === this.data.answer ? 1 : 2,
                    disable: !0,
                    answerProgress: 3
                }), t.abrupt("return");

              case 23:
                this.setData({
                    answerState: s === this.data.answer ? 1 : 2,
                    disable: !0,
                    answerProgress: 1
                });

              case 24:
                t.next = 30;
                break;

              case 26:
                t.prev = 26, t.t0 = t.catch(9), console.log(t.t0), wx.showToast({
                    title: "网络异常",
                    icon: "none",
                    duration: 1e3
                });

              case 30:
              case "end":
                return t.stop();
            }
        }, t, this, [ [ 9, 26 ] ]);
    })), function(t) {
        return a.apply(this, arguments);
    }),
    nextQuestion: function() {
        this.data.disable ? (this.setData({
            answerState: 0,
            correctIndex: -1,
            errorIndex: -1,
            disable: !1,
            completeNum: this.data.indexNum
        }), this.getQuestion()) : wx.showToast({
            title: "尚未选择答案",
            icon: "none",
            duration: 1e3
        });
    },
    nonMultipleComplete: (e = c(d.default.mark(function t() {
        var e, a;
        return d.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                if (!this.data.disable) {
                    t.next = 13;
                    break;
                }
                if (e = new Date(), a = wx.getStorageSync("openid"), this.data.activityId) {
                    t.next = 8;
                    break;
                }
                return t.next = 6, l.default.finishAnswer({
                    openid: a,
                    totalTime: e - this.data.startTime,
                    type: 4
                });

              case 6:
                t.next = 10;
                break;

              case 8:
                return t.next = 10, l.default.activityFinish({
                    openid: a,
                    activityId: this.data.activityId,
                    totalTime: e - this.data.startTime,
                    type: 4
                });

              case 10:
                this.setData({
                    completeNum: this.data.endNum,
                    finished: !0
                }), t.next = 14;
                break;

              case 13:
                wx.showToast({
                    title: "尚未选择答案",
                    icon: "none",
                    duration: 1e3
                });

              case 14:
              case "end":
                return t.stop();
            }
        }, t, this);
    })), function() {
        return e.apply(this, arguments);
    }),
    multipleComplete: (t = c(d.default.mark(function t() {
        var e, a;
        return d.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                if (e = new Date(), a = wx.getStorageSync("openid"), this.data.activityId) {
                    t.next = 7;
                    break;
                }
                return t.next = 5, l.default.finishAnswer({
                    openid: a,
                    totalTime: e - this.data.startTime,
                    type: 4
                });

              case 5:
                t.next = 9;
                break;

              case 7:
                return t.next = 9, l.default.activityFinish({
                    openid: a,
                    activityId: this.data.activityId,
                    totalTime: e - this.data.startTime,
                    type: 4
                });

              case 9:
                this.setData({
                    completeNum: this.data.endNum,
                    finished: !0
                });

              case 10:
              case "end":
                return t.stop();
            }
        }, t, this);
    })), function() {
        return t.apply(this, arguments);
    })
});